package com.juguo.charginganimation.View.HomeFragment;

import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.juguo.charginganimation.Base.BaseFragment;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.utils.MyViewPager;
import com.juguo.charginganimation.View.gallery.Gallery;
import com.juguo.charginganimation.Viewmodel.FeaturedViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;


public class FeaturedFragment extends BaseFragment {

    private String TAG = "FeaturedFragment";
    private FeaturedViewModel mViewModel;
    private Context mContext;

    //图片瀑布流
    private MyViewPager viewPager;

    public static FeaturedFragment newInstance() {
        return new FeaturedFragment();
    }

    @Override
    public void onAttach(@NonNull @NotNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.featured_fragment, container, false);
        viewPager = root.findViewById(R.id.viewpage);
        return root;
    }

    @Override
    protected int initLayout() {
        return R.layout.featured_fragment;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(FeaturedViewModel.class);
        initViewPage();
    }


    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume()");
        onCreate(null);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    /**
     * viewPage初始化
     */
    private void initViewPage() {

        //fragment数据源
        final ArrayList<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new Gallery());
        viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        //预加载界面
        viewPager.setOffscreenPageLimit(fragmentList.size() - 1);
    }

}